package com.qst.rongxiaoserver.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qst.rongxiaoserver.common.Result;
import com.qst.rongxiaoserver.model.ProductModel;
import com.qst.rongxiaoserver.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jdk.jfr.Description;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/product")
@Tag(name = "商品管理", description = "商品管理")
public class ProductController {

    @Autowired
  IProductService productService;

    // 添加商品
    @PostMapping("/add")
    @Operation(summary = "添加商品", description = "添加商品")
    public Result addProduct(@Valid @RequestBody ProductModel product) {
        boolean isSave =  productService.save(product);

        return isSave ? Result.success("添加成功") : Result.fail("添加失败");
    }

    //修改产品
    @PostMapping("/update")
    @Operation(summary = "修改商品", description = "修改商品")
    public Result updateProduct(@Valid @RequestBody ProductModel product) {
        boolean isSave =  productService.updateById(product);
        return isSave? Result.success("修改成功") : Result.fail("修改失败");
    }

    // 删除
    @DeleteMapping("/delete")
    @Operation(summary = "删除商品", description = "删除商品")
    public Result deleteProduct(@Valid @RequestParam Integer id) {
        boolean isDelete =  productService.removeById(id);
        return isDelete? Result.success("删除成功") : Result.fail("删除失败");
    }

    // 分页查询
    @GetMapping("/list")
    @Operation(summary = "查询所有商品", description = "查询所有商品")
    public Result listProduct(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {

       Page<ProductModel> datas =  productService.getProductByPage(pageNum,pageSize);

        return Result.success(datas);
    }

    // 商品详情
    @GetMapping("/detail")
    @Operation(summary = "查询商品详情", description = "查询商品详情")
    public Result detailProduct(@RequestParam Integer id) {
        ProductModel product = productService.getById(id);
        return Result.success(product);
    }
}
